package tech.mobi.child;

import java.util.ArrayList;
import java.util.HashMap;

import tech.mobi.child.adapter.JYGYClientListAdapter;
import tech.mobi.child.base.BaseAct;
import tech.mobi.child.bean.JYGYClientBean;
import tech.mobi.child.service.MainService;
import tech.mobi.child.utils.Configuration;
import tech.mobi.child.utils.OSCPullToRefreshListView;
import tech.mobi.child.utils.Task;
import tech.mobi.child.utils.TaskType;
import tech.mobi.child.utils.Tools;
import tech.mobi.child.utils.User;
import tech.mobi.child.utils.OSCPullToRefreshListView.OnRefreshListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class JYGYClientListMainAct extends BaseAct implements OnClickListener {

	private Button btnBack;
	private Button btnUpload;
	
	private TextView tvTitle;
	private ProgressBar progress;
	
	private OSCPullToRefreshListView listView = null;
	private JYGYClientListAdapter<JYGYClientBean> adapter;
	private ArrayList<JYGYClientBean> groupList = new ArrayList<JYGYClientBean>();
	private LinearLayout llLoading;
	private RelativeLayout rlLoading;
	private Button btnReload;
//	private ArrayList<GroupBean> data;
//	private LayoutInflater inflater;

	public static int currentPageNumber = 1;
	private String strCacheKey = "KeyJYGYClientList";
	private String strType = "%";
	private String strOrder = "ZXFB";
	private String strWhere = "";
	
	private View lvQuestion_footer;
	private TextView lvQuestion_foot_more;
	private ProgressBar lvQuestion_foot_progress; 

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jygy_client_main);

		progress = (ProgressBar) this.findViewById(R.id.progress);
		tvTitle = (TextView) this.findViewById(R.id.tvTitle);
		btnBack = (Button) this.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);
		btnUpload = (Button) this.findViewById(R.id.btnUpload);
		btnUpload.setOnClickListener(this);
		
		User.userName = "13731525124";
		// 正在获取信息 和 获取信息按钮 集合
		llLoading = (LinearLayout) this.findViewById(R.id.llLoading);
		rlLoading = (RelativeLayout) this.findViewById(R.id.rlLoading); // 正在获取信息
																		// 集合
		btnReload = (Button) this.findViewById(R.id.btnReload); // 获取信息 按钮
		btnReload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				btnReload.setVisibility(View.GONE);
				rlLoading.setVisibility(View.VISIBLE);
				initData();
			}
		});
		currentPageNumber = 1;

		groupList = (ArrayList<JYGYClientBean>) MainService
				.getCacheData(User.userName + this.strCacheKey);
		if (groupList == null) {
			setClickEnable(false);
			initData();
		} else {
			initGroupListView();
			llLoading.setVisibility(View.GONE);
			String updateTime = MainService.getCacheDataTime(User.userName
					+ this.strCacheKey);
			this.adapter.addData(groupList);
			//new Date().toLocaleString()
			this.listView.onRefreshComplete(getString(R.string.pull_to_refresh_update) + updateTime);
			this.listView.setSelection(0);
			
			if(groupList.size()==0) {
				listView.setTag(4);
				lvQuestion_foot_more.setText(R.string.load_empty);
			} else if(groupList.size()<Integer.parseInt(Configuration.PAGE_SIZE)) {
				listView.setTag(3); //UIHelper.LISTVIEW_DATA_FULL size < 10
				adapter.notifyDataSetChanged();
				lvQuestion_foot_progress.setVisibility(View.GONE);
				lvQuestion_foot_more.setText(R.string.load_full);
			} else if (groupList.size()>=Integer.parseInt(Configuration.PAGE_SIZE)){
				listView.setTag(1);
				adapter.notifyDataSetChanged();
				lvQuestion_foot_more.setText(R.string.load_more);
			}
			
		}
	}
	
	private void initGroupListView() {
		if (listView == null) {
			
			listView = (OSCPullToRefreshListView) this.findViewById(R.id.listView);
			final ArrayList<JYGYClientBean> groupList = new ArrayList<JYGYClientBean>();
			adapter = (JYGYClientListAdapter<JYGYClientBean>) new JYGYClientListAdapter<JYGYClientBean>(
					groupList, listView, this);
			
	        lvQuestion_footer = getLayoutInflater().inflate(R.layout.osc_listview_footer, null);
	        lvQuestion_foot_more = (TextView)lvQuestion_footer.findViewById(R.id.listview_foot_more);
	        lvQuestion_foot_progress = (ProgressBar)lvQuestion_footer.findViewById(R.id.listview_foot_progress);
	        listView.addFooterView(lvQuestion_footer);//添加底部视图  必须在setAdapter前
			listView.setAdapter(adapter);
			
		}
	}

	public void initData() {
		setClickEnable(false);
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("a", User.userName);
		map.put("c", currentPageNumber + "");
		map.put("d", Configuration.PAGE_SIZE);
		map.put("e", "0");// 0-代表第一次请求数据
		map.put("b", "1");// more-代表获取更多信息，notmore－代表下拉刷新获取数据
		map.put("typeid", strType);
		map.put("orderid", strOrder);
		map.put("wheretext", strWhere);
		map.put("clientVersion", getClientVersionNumber());
		Task task = new Task(TaskType.TASK_JYGY_CLIENT_LIST_INIT, map);
		MainService.newTask(task);
	}

	@Override
	public void init() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object... param) {
		stopProgress();
		String isLastPage = "true";
		String updateTime = "";
		int type_id = (Integer) param[0];
		switch (type_id) {
		case TaskType.TASK_JYGY_CLIENT_LIST_INIT: // 首次加载刷新
			// TODO 首次加载进行数据刷新操作
			setClickEnable(true);
			try {
				HashMap<String, Object> map = (HashMap<String, Object>) param[1];
				if (map.size() == 2) {
					llLoading.setVisibility(View.GONE);
					HashMap<String, Object> map1 = new HashMap<String, Object>();
					map1.put(MainAct.LAST_UPDATE_DATE, "");
					map1.put(MainAct.LAST_UPDATE_DATA,
							new ArrayList<JYGYClientBean>());
					MainService.newCacheData(User.userName + this.strCacheKey,
							map1);

					initGroupListView();
					updateTime = map.get("updatetime").toString();
					MainService.updateCacheDataTime(User.userName
							+ this.strCacheKey, updateTime);
					isLastPage = loadData((ArrayList<JYGYClientBean>) map
							.get("data"));

					
					this.listView.onRefreshComplete(getString(R.string.pull_to_refresh_update) + updateTime);
					this.listView.setSelection(0);
					
					ArrayList<JYGYClientBean> groupList = (ArrayList<JYGYClientBean>) map
					.get("data");
					if(groupList.size()==0) {
						listView.setTag(4);
						lvQuestion_foot_more.setText(R.string.load_empty);
					} else if(groupList.size()<Integer.parseInt(Configuration.PAGE_SIZE)) {
						listView.setTag(3); //UIHelper.LISTVIEW_DATA_FULL size < 10
						adapter.notifyDataSetChanged();
						lvQuestion_foot_progress.setVisibility(View.GONE);
						lvQuestion_foot_more.setText(R.string.load_full);
					} else if (groupList.size()>=Integer.parseInt(Configuration.PAGE_SIZE)){
						listView.setTag(1);
						adapter.notifyDataSetChanged();
						lvQuestion_foot_more.setText(R.string.load_more);
					}
					
					
				} else {
					rlLoading.setVisibility(View.GONE);
					btnReload.setVisibility(View.VISIBLE);
//					Toast.makeText(this, "网络环境不好，请稍候再试", Toast.LENGTH_LONG)
//							.show();
				}
			} catch (Exception ex) {

			}
			break;
		}

	}


	@SuppressWarnings("unchecked")
	public String loadData(ArrayList<JYGYClientBean> groupList) {
		String isLastPage = "true";

		if (groupList.size() > 0) {
			MainService.addCacheData(User.userName + this.strCacheKey,
					groupList);
			this.adapter.addData(groupList);
			currentPageNumber++;
			GoodClient.strCurrCount = currentPageNumber +"";
			if (!(groupList.size() < Integer.parseInt(Configuration.PAGE_SIZE))) {
				isLastPage = "false";
			}
		}
		return isLastPage;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnBack) {
			finish();
		} else if (v.getId() ==R.id.btnUpload) {
			//CZDAClientListTxtAddMainAct
//			Intent intent = new Intent(this,CZDAClientListTxtAddMainAct.class);
//			intent.putExtra("typeId", strTypeId);
//			intent.putExtra("titleName", strTitle);
//			startActivity(intent);
		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void setClickEnable(boolean bool) {
		btnBack.setEnabled(bool);
		btnUpload.setEnabled(bool);
		if (bool) {
			this.progress.setVisibility(View.GONE);
		} else {
			this.progress.setVisibility(View.VISIBLE);
		}
		
	}

	public static class GoodClient {

		/**
		 * 订购
		 */
		public static String strTypeId = "";
		public static String strTypeName = "";

		public static String strOrderId = "";
		public static String strOrderName = "";
		
		public static String strCurrCount = "";
		
		public static boolean isOpen = false;

	}

}
